<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller 
{
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/Member');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Admin/Member');
    		if($model->validate($model->_update_validate)->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst?id='.session('m_id')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Member');
    	$data = $model->find($id);
    	$this->assign('data', $data);

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }

    public function lst()
    {
        $memberId = session('m_id');
        if(!$memberId){
            session('returnUrl',U('Index/index'));
            redirect(U('Member/login'));
        }

        $id = I('get.id');
    	$model = D('Admin/Member');
    	$data = $model->find($id);
    	$this->assign(array(
    		'data' => $data,
    	));
		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '列表',
			'_page_btn_name' => '添加',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }
    //验证码
    public function chkcode(){
        $Verify = new \Think\Verify(array(
            'fontSize' => 30,
            'length' => 4,
            'userNoise' => true,
        ));
        $Verify->entry();
    }
    //登录
    public function login(){
        if(IS_POST){
            $model = D('Admin/Member');
            if($model->validate($model->_login_validate)->create()){
                if($model->login()){
                    //默认跳转地址
                    $returnUrl = U('/');
                    $ru = session('returnUrl');
                    if($ru){
                        session('returnUrl',null);
                        $returnUrl = $ru;
                    }
                    $this->success('登录成功！',$returnUrl);
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $this->assign(array(
            '_page_title' => '登录',
            '_page_keywords' => '登录',
            '_page_description' => '登录',
        ));
        $this->display();
    }

    //注册
    public function regist(){
        if(IS_POST){
            $model = D('Admin/Member');
            if($model->create(I('post.'),1)){
                if($model->add()){
                    $model->username = I('post.username');
                    $model->login(false);
                    $this->success('注册成功！',U('/'));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        $this->assign(array(
            '_page_title' => '注册',
            '_page_keywords' => '注册',
            '_page_description' => '注册',
        ));
        $this->display();
    }

    //登出
    public function logout(){
        $model = D('Admin/Member');
        $model->logout();
        redirect('/');
    }

    public function ajaxChkLogin(){
        if(session('m_id')){
            echo json_encode(array(
                'login' => 1,
                'username' => session('m_username'),
                'nickname' => session('m_nickname'),
            ));
        } else {
            echo json_encode(array(
                'login' => 0,
            ));
        }
    }
}